package com.gxa.guahaoclient02.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service("guaHaoJiLu")
public class GuaHaoJiLuService {
    private RedisTemplate<String, Object> redisTemplate;

    @Autowired
    public GuaHaoJiLuService(RedisTemplate<String, Object> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    public GuaHaoJiLuService() {

    }

    /**
     *
     * @return 返回当日挂号人数
     */
    public Map<String, Object> chaXunDayCount() {
        Map<String, Object> map = new HashMap<>();
        // 前端端分离时，前端人员会首先判断code值是否满足200，如果不是200，则提醒用户失败
        map.put("code", 200);
        map.put("msg", "查询成功");
        System.out.println(redisTemplate.opsForValue().get("mouse"));
        System.out.println(redisTemplate.opsForZSet().range("guahaocount",0,-1));
        map.put("todaycount",redisTemplate.opsForZSet().score("guahaocount","2021:03:17"));
        System.out.println("今日挂号人数"+new SimpleDateFormat("yyyy:MM:dd").format(new Date(System.currentTimeMillis()))+ "***" + redisTemplate.opsForZSet().score("guahaocount",new SimpleDateFormat("yyyy:MM:dd").format(new Date(System.currentTimeMillis()))));
        map.put("yesterdaycount",redisTemplate.opsForZSet().score("guahaocount",new SimpleDateFormat("yyyy:MM:dd").format(new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24))));

        return map;
    }

    /**
     *
     * @return 返回当日挂号金额
     */
    public Map<String, Object> chaXunDayPayment() {
        Map<String, Object> map = new HashMap<>();
        // 前端端分离时，前端人员会首先判断code值是否满足200，如果不是200，则提醒用户失败
        map.put("code", 200);
        map.put("msg", "查询成功");
//        System.out.println(redisTemplate.opsForValue().get("mouse"));
        map.put("todaypayment",redisTemplate.opsForZSet().score("ghuahaopayment",new SimpleDateFormat("yyyy:MM:dd").format(new Date(System.currentTimeMillis()))));
        map.put("yesterdaypayment",redisTemplate.opsForZSet().score("ghuahaopayment",new SimpleDateFormat("yyyy:MM:dd").format(new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24))));

        return map;
    }

    /**
     *
     * @param id 当前操作账号的id
     * @return  返回当前账号挂号金额日统计
     */
    public Map<String, Object> chaXunDayPaymentById(String id) {
        Map<String, Object> map = new HashMap<>();
        // 前端端分离时，前端人员会首先判断code值是否满足200，如果不是200，则提醒用户失败
        map.put("code", 200);
        map.put("msg", "查询成功");
//        System.out.println(redisTemplate.opsForValue().get("mouse"));
        map.put("accounttodaypayment",redisTemplate.opsForZSet().score("ghuahaopayment",new SimpleDateFormat("yyyy:MM:dd").format(new Date(System.currentTimeMillis()))+ ":" + id) );
        map.put("accountyesterdaypayment",redisTemplate.opsForZSet().score("ghuahaopayment",new SimpleDateFormat("yyyy:MM:dd").format(new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24L))+ ":" + id) );

        return map;
    }


    /**
     *
     * @return 返回当月挂号人数
     */
    public Map<String, Object> chaXunMonthCount() {
        Map<String, Object> map = new HashMap<>();
        // 前端端分离时，前端人员会首先判断code值是否满足200，如果不是200，则提醒用户失败
        map.put("code", 200);
        map.put("msg", "查询成功");
        System.out.println(redisTemplate.opsForValue().get("mouse"));
        map.put("monthcount",redisTemplate.opsForZSet().score("guahaocount",new SimpleDateFormat("yyyy:MM").format(new Date(System.currentTimeMillis()))));
        map.put("lastmonthpayment",redisTemplate.opsForZSet().score("guahaocount",new SimpleDateFormat("yyyy:MM").format(new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24 * 32L))));

        return map;
    }

    /**
     * @return 返回当月挂号金额
     */
    public Map<String, Object> chaXunMonthPayment() {
        Map<String, Object> map = new HashMap<>();
        // 前端端分离时，前端人员会首先判断code值是否满足200，如果不是200，则提醒用户失败
        map.put("code", 200);
        map.put("msg", "查询成功");
//        System.out.println(redisTemplate.opsForValue().get("mouse"));
        map.put("monthpayment",redisTemplate.opsForZSet().score("ghuahaopayment",new SimpleDateFormat("yyyy:MM").format(new Date(System.currentTimeMillis()))));
        map.put("lastmonthpayment",redisTemplate.opsForZSet().score("ghuahaopayment",new SimpleDateFormat("yyyy:MM").format(new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24 * 32L))));

        return map;
    }

    /**
     *
     * @param id 当前操作账号的id
     * @return 返回当前账号挂号金额月统计
     */
    public Map<String, Object> chaXunMonthPaymentById(String id) {
        Map<String, Object> map = new HashMap<>();
        // 前端端分离时，前端人员会首先判断code值是否满足200，如果不是200，则提醒用户失败
        map.put("code", 200);
        map.put("msg", "查询成功");
//        System.out.println(redisTemplate.opsForValue().get("mouse"));
        map.put("accountmonthpayment",redisTemplate.opsForZSet().score("ghuahaopayment",new SimpleDateFormat("yyyy:MM").format(new Date(System.currentTimeMillis()))+ ":" + id));
        map.put("accountlastmonthpayment",redisTemplate.opsForZSet().score("ghuahaopayment",new SimpleDateFormat("yyyy:MM").format(new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24 * 32L))+ ":" + id));

        return map;

    }


    /**
     * @return 返回当月挂号金额记录
     */
    public Map<String, Object> chaXunMonthPaymentRecord() {
        Map<String, Object> map = new HashMap<>();
        // 前端端分离时，前端人员会首先判断code值是否满足200，如果不是200，则提醒用户失败
        map.put("code", 200);
        map.put("msg", "查询成功");
//        System.out.println(redisTemplate.opsForValue().get("mouse"));
        map.put("monthcount",redisTemplate.opsForZSet().score("guahaocount",
                new SimpleDateFormat("yyyy:MM").format(new Date(System.currentTimeMillis()))));
        map.put("lastmonthpayment",redisTemplate.opsForZSet().score("guahaocount",
                new SimpleDateFormat("yyyy:MM").format(new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24 * 32L))));
        Map<String,Object> datalist = new HashMap<>();
        long currenttime = System.currentTimeMillis();
        SimpleDateFormat df = new SimpleDateFormat("yyyy:MM:dd");//设置日期格式
        String date = df.format(currenttime);
        int index = Integer.parseInt(date.substring(8));
        for (int i = 1; i <= Integer.parseInt(date.substring(8)) ; i++){
            System.out.println(" date: " + date + " index:" + index + "  " + redisTemplate.opsForZSet().score("guahaocount",date) +"******");
            if(redisTemplate.opsForZSet().score("guahaocount",date) != null){
                datalist.put(index+"",redisTemplate.opsForZSet().score("guahaocount",date) );
            }
            index--;
            currenttime -= 1000*60*60*24L;
            date = df.format(currenttime);
            System.out.println(date);
        }

        map.put("data",datalist);

        return map;
    }

    /**
     *
     * @return 返回当月挂号人数记录
     */
    public Map<String, Object> chaXunMonthCountRecord() {
        Map<String, Object> map = new HashMap<>();
        // 前端端分离时，前端人员会首先判断code值是否满足200，如果不是200，则提醒用户失败
        map.put("code", 200);
        map.put("msg", "查询成功");
//        System.out.println(redisTemplate.opsForValue().get("mouse"));
        map.put("monthpayment",redisTemplate.opsForZSet().score("ghuahaopayment",
                new SimpleDateFormat("yyyy:MM").format(new Date(System.currentTimeMillis()))));
        map.put("lastmonthpayment",redisTemplate.opsForZSet().score("ghuahaopayment",
                new SimpleDateFormat("yyyy:MM").format(new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24 * 32L))));
        Map<String,Object> datalist = new HashMap<>();
        long currenttime = System.currentTimeMillis();
        SimpleDateFormat df = new SimpleDateFormat("yyyy:MM:dd");//设置日期格式
        String date = df.format(currenttime);
        int index = Integer.parseInt(date.substring(8));
        for (int i = 1; i <= Integer.parseInt(date.substring(8)) ; i++){
            System.out.println(" date: " + date + " index:" + index + "  " + redisTemplate.opsForZSet().score("ghuahaopayment",date) +"******");
           if (redisTemplate.opsForZSet().score("ghuahaopayment",date) != null){
               datalist.put(index+"",redisTemplate.opsForZSet().score("ghuahaopayment",date));
           }
            index--;
            currenttime -= 1000*60*60*24L;
            date = df.format(currenttime);
            System.out.println(date);
        }

        map.put("data",datalist);

        return map;
    }
    public static void main(String[] args) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy:MM:dd");//设置日期格式
        String logintime = df.format(new Date(System.currentTimeMillis()));
        System.out.println(logintime);
         df = new SimpleDateFormat("yyyy:MM");//设置日期格式
         logintime = df.format(new Date(System.currentTimeMillis() - (1000 * 60 * 60 * 24 * 31L)));
        System.out.println();
        GuaHaoJiLuService guaHaoJiLuService = new GuaHaoJiLuService();
        System.out.println(guaHaoJiLuService.redisTemplate.opsForZSet().score("guahaocount","2021:03:17"));
        System.out.println(logintime);

//    System.out.println(new GuaHaoJiLu().redisTemplate.opsForZSet().score("guahaogeshu",new SimpleDateFormat().format(new Date(System.currentTimeMillis()))));
    }

    public void check() {

       redisTemplate.opsForZSet().add("guahaocount","2021:02:18",1);
       redisTemplate.opsForZSet().add("guahaocount","2021:02:19",1);
       redisTemplate.opsForZSet().add("guahaocount","2021:03:10",1);
       redisTemplate.opsForZSet().add("guahaocount","2021:03:13",1);
       redisTemplate.opsForZSet().add("guahaocount","2021:03:17",1);
       redisTemplate.opsForZSet().add("guahaocount","2021:02:16",2);
       redisTemplate.opsForZSet().add("guahaocount","2021:03:11",2);
       redisTemplate.opsForZSet().add("guahaocount","2021:03:16",3);
       redisTemplate.opsForZSet().add("guahaocount","2021:02",5);
       redisTemplate.opsForZSet().add("guahaocount","2021:03",8);

       redisTemplate.opsForZSet().add("ghuahaopayment","2021:02:16:1",50);
       redisTemplate.opsForZSet().add("ghuahaopayment","2021:02:16:3",50);
       redisTemplate.opsForZSet().add("ghuahaopayment","2021:02:17",50);
       redisTemplate.opsForZSet().add("ghuahaopayment","2021:02:3",50);
       redisTemplate.opsForZSet().add("ghuahaopayment","2021:03:1",50);
       redisTemplate.opsForZSet().add("ghuahaopayment","2021:03:16:1",50);
       redisTemplate.opsForZSet().add("ghuahaopayment","2021:03:16:2",50);
       redisTemplate.opsForZSet().add("ghuahaopayment","2021:03:17:4",50);
       redisTemplate.opsForZSet().add("ghuahaopayment","2021:02:16:4",60);
       redisTemplate.opsForZSet().add("ghuahaopayment","2021:02:18",60);
       redisTemplate.opsForZSet().add("ghuahaopayment","2021:02:19",60);
       redisTemplate.opsForZSet().add("ghuahaopayment","2021:02:19:1",60);
       redisTemplate.opsForZSet().add("ghuahaopayment","2021:02:4",60);
       redisTemplate.opsForZSet().add("ghuahaopayment","2021:03:11:2",60);
       redisTemplate.opsForZSet().add("ghuahaopayment","2021:03:17",60);
       redisTemplate.opsForZSet().add("ghuahaopayment","2021:03:17:3",60);
       redisTemplate.opsForZSet().add("ghuahaopayment","2021:03:3",60);
       redisTemplate.opsForZSet().add("ghuahaopayment","2021:03:10",70);
       redisTemplate.opsForZSet().add("ghuahaopayment","2021:03:10:5",70);
       redisTemplate.opsForZSet().add("ghuahaopayment","2021:03:11:4",70);
       redisTemplate.opsForZSet().add("ghuahaopayment","2021:03:13",70);
       redisTemplate.opsForZSet().add("ghuahaopayment","2021:03:13:2",70);
       redisTemplate.opsForZSet().add("ghuahaopayment","2021:03:16:5",70);
       redisTemplate.opsForZSet().add("ghuahaopayment","2021:02:16",100);
       redisTemplate.opsForZSet().add("ghuahaopayment","2021:02:1",110);
       redisTemplate.opsForZSet().add("ghuahaopayment","2021:03:11",120);
       redisTemplate.opsForZSet().add("ghuahaopayment","2021:03:4",120);
       redisTemplate.opsForZSet().add("ghuahaopayment","2021:03:5",140);
       redisTemplate.opsForZSet().add("ghuahaopayment","2021:03:16",170);
       redisTemplate.opsForZSet().add("ghuahaopayment","2021:03:2",180);
       redisTemplate.opsForZSet().add("ghuahaopayment","2021:02",270);
       redisTemplate.opsForZSet().add("ghuahaopayment","2021:03",490);

        System.out.println("今日挂号人数"+new SimpleDateFormat("yyyy:MM:dd").format(new Date(System.currentTimeMillis()))+ "***" + redisTemplate.opsForZSet().score("guahaogeshu",new SimpleDateFormat("yyyy:MM:dd").format(new Date(System.currentTimeMillis()))));

    }
}
